from pymysql import Connection


def getCon():
    con = Connection(
        host='localhost',
        port=3306,
        user='root',
        password='123456',
        database='db_book',
        autocommit=True
    )
    return con


def closeCon(con: Connection):
    """
    关闭数据库链接
    :param con:
    :return:
    """
    if con:
        con.close()


if __name__ == '__main__':
    con = None
    try:
        con = getCon()
        cursor = con.cursor()
        cursor.execute("select * from t_user")
        print(cursor.fetchall())
    except Exception as ex:
        print(ex)
    finally:
        closeCon(con)
